package com.crm.controller.before_sale;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.crm.pojo.PlanDetail;
import com.crm.pojo.SalePlan;
import com.crm.service.before_sale.SaleChanceService;
import com.crm.service.before_sale.SalePlanService;
import com.crm.vo.Result;
import com.crm.vo.SalePlanDetailsVO;
import com.crm.vo.SalePlanPlanDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * 销售计划
 */
@Controller
public class SalePanController {
    @Autowired
    SalePlanService salePlanService;

    //计划表
    @GetMapping("zjk-salePlan")
    public String toSalePlanDatagrid() {
        return "before-sale/salePlan";
    }

    //计划表单
    @GetMapping("zjk-salePlan-form")
    public String toSalePlanFrom() {
        return "before-sale/salePlan-form";
    }

    //明细表单
    @GetMapping("add-salePlan-details")
    public String toSalePlanDetails() {
        return "before-sale/add-salePlan-edtalis";
    }

    //添加明细窗口
    @GetMapping("add-and-edit-planDetails")
    public String toAddAndEditPlanDetails() {
        return "before-sale/salePlan-add-and-edit-planDetails";
    }

    //编辑计划窗口
    @GetMapping("salePlan-and-planDetails-edit-form")
    public String toEditPlanDetails() {
        return "before-sale/salePlan-and-planDetails-edit-form";
    }

    //编辑和添加明细窗口
    @GetMapping("edit-and-add-planDetails-f")
    public String toEditAndAddPlanDetails() {
        return "before-sale/edit-and-add-planDetails-f";
    }

    //纯编辑明细窗口

    @GetMapping("chun-edit-planDetails-f")
    public String toChunEditPlanDetails() {
        return "before-sale/chun-edit-planDetails-f";
    }


    /**
     * 分页展示销售计划列表
     *
     * @param salePlan
     * @param page
     * @param rows
     * @param request
     * @return
     */
    @PostMapping("find-all-salePlan")
    @ResponseBody
    public Map findAllSalePlan(SalePlan salePlan, int page, int rows, HttpServletRequest request) {
        List<SalePlan> list = salePlanService.getSalePlanList(salePlan, page, rows, null, null, null,
                null, null, null);
        Long total = salePlanService.getSalePlanCount(salePlan, null, null, null, null,
                null, null);

        Map map = new HashMap();
        map.put("rows", list);
        map.put("total", total);
        return map;
    }

    /**
     * 高级查询 销售计划
     *
     * @param salePlan
     * @param page
     * @param rows
     * @param minMoney
     * @param maxMoney
     * @param createStartTime
     * @param createEndTime
     * @param updateStartTime
     * @param updateEndTime
     * @return
     */
    @PostMapping("advance-salePlan")
    @ResponseBody
    public Map advanceSalePlan(SalePlan salePlan, int page, int rows, BigDecimal minMoney, BigDecimal maxMoney, Date createStartTime, Date createEndTime,
                               Date updateStartTime, Date updateEndTime) {

        List<SalePlan> list = salePlanService.getSalePlanList(salePlan, page, rows, minMoney, maxMoney, createStartTime, createEndTime, updateStartTime, updateEndTime);
        Long total = salePlanService.getSalePlanCount(salePlan, minMoney, maxMoney, createStartTime, createEndTime, updateStartTime, updateEndTime);
        Map map = new HashMap();
        map.put("rows", list);
        map.put("total", total);
        return map;
    }

    /**
     * 展示一个销售计划对应的计划明细
     *
     * @param planId
     * @return
     */
    @PostMapping("getSalePlanDetailsList")
    @ResponseBody
    public List<SalePlanDetailsVO> getSalePlanDetailsList(Long planId) {
        Long id = planId == null ? 0L : planId;
        return salePlanService.getSalePlanDetailsList(id);
    }

    /**
     * 根据id删除销售计划
     *
     * @param planId
     * @return
     */
    @PostMapping("deleteSalePlan")
    @ResponseBody
    public Result deleteSalePlan(String planId) {
        Long id = planId == null ? 0L : new Long(planId);
        return salePlanService.deleteSalePlanById(id);
    }

    /**
     * 添加一个销售计划
     *
     * @param planName
     * @param userId
     * @param planCheck
     * @param strategyProduct
     * @param strategyPrice
     * @param expenseBudget
     * @return
     */
    @PostMapping("add-salePlan")
    @ResponseBody
    public Long addSalePlan(String planName, BigDecimal userId, String planCheck, String strategyProduct, String strategyPrice,
                            BigDecimal expenseBudget, Date setTime, Date updateDate) {
        try {

            return salePlanService.addSalePlan(planName, userId, planCheck, strategyProduct, strategyPrice, expenseBudget, setTime, updateDate).getPlanId();
        } catch (Exception e) {
            e.printStackTrace();
            return new Long(0);
        }
    }

    /**
     * 添加销售计划对应的计划明细
     *
     * @param planDetail
     * @param id
     * @return
     */
    @GetMapping("add-planDetails")
    @ResponseBody
    public Result addPlanDetails(PlanDetail planDetail, Long id) {
        id = id == null ? 0 : id;
        if (planDetail != null) {
            salePlanService.addPlanDetails(planDetail, id);
            return Result.SUCCESS;
        } else {
            return new Result();
        }

    }

    /**
     * 根据id查找计划
     *
     * @param id
     * @return
     */
    @PostMapping("findSalePlanById")
    @ResponseBody
    public SalePlan findSalePlanById(Long id) {
        return salePlanService.findOneSalePlanById(id);
    }

    /**
     * 根据id获取一条计划明细
     *
     * @param saleDetId
     * @return
     */
    @PostMapping("getOnePlanDetails")
    @ResponseBody
    public SalePlanDetailsVO getOnePlanDetails(Long saleDetId) {
        saleDetId = saleDetId == null ? 0 : saleDetId;
        return salePlanService.getOnePlanDetails(saleDetId);
    }


    /**
     * 编辑计划明细
     *
     * @param planDetail
     * @return
     */
    @PostMapping("editPlanDetails")
    @ResponseBody
    public Result editPlanDetailsById(PlanDetail planDetail) {
        if (planDetail != null) {
            PlanDetail p = salePlanService.getOnePlanDetails(planDetail.getSaleDetId()).getPlanDetail();

            p.setExpectGain(planDetail.getExpectGain());
            p.setPlanMoney(planDetail.getPlanMoney());
            p.setPlanTime(planDetail.getPlanTime());
            salePlanService.savePlanDetails(p);
            return Result.SUCCESS;
        } else {
            return new Result();
        }
    }

    /**
     * 获取一个计划下的明细列表
     *
     * @param pid
     * @return
     */
    @GetMapping("getPlanDetailsBySalePlanId")
    @ResponseBody
    public List<SalePlanDetailsVO> getPlanDetailsBySalePlanId(Long pid) {
        List<SalePlanDetailsVO> list = salePlanService.getPlanDetailsList(pid);
        return list;

    }

    /**
     * 删除计划明细
     *
     * @param saleDetId
     * @return
     */
    @PostMapping("zjk-deletePlanDetails")
    @ResponseBody
    public Result deletePlanDetails(Long planId, String rows) {
        if (planId != null && rows != null) {
            List<SalePlanDetailsVO> list = salePlanService.getPlanDetailsList(planId);
            List<PlanDetail> planDetailList = new ArrayList<>();
            JSONObject jsonObject = JSON.parseObject(rows);
            BigDecimal b = new BigDecimal(jsonObject.getJSONObject("planDetail").get("saleDetId").toString());
            for (SalePlanDetailsVO salePlanDetailsVO : list) {
                if (new BigDecimal(salePlanDetailsVO.getPlanDetail().getSaleDetId().toString()).equals(b)) {
                    salePlanDetailsVO.getPlanDetail().setPlanDetailIsDel("是");
                    planDetailList.add(salePlanDetailsVO.getPlanDetail());
                }
            }
            salePlanService.savePlanDetails(planDetailList);
            return Result.SUCCESS;

        } else {
            return new Result("参数解析错误！");
        }

    }

    /**
     * 编辑计划
     *
     * @param salePlan
     * @return
     */
    @PostMapping("zjk-editSalePlan")
    @ResponseBody
    public Result editSalePlan(SalePlan salePlan) {
        return salePlanService.editSalePlan(salePlan);
    }

    /**
     * 删除明细
     *
     * @param planDetail
     * @return
     */
    @PostMapping("new-DeletePlanDetail")
    @ResponseBody
    public Result newDeletePlanDetail(PlanDetail planDetail) {
        return salePlanService.newDeletePlanDetail(planDetail);
    }
}
